package com.yicong.ants.scenic.viewModel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.cchao.simplelib.core.RxHelper
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity
import com.yicong.ants.manager.http.RetrofitHelper
import com.yicong.ants.scenic.data.CityInfo
import com.yicong.ants.scenic.data.LoadStateData
import com.yicong.ants.scenic.data.OneTimeLiveData
import com.yicong.ants.scenic.data.PageData
import com.yicong.ants.scenic.data.ScenicDetail
import com.yicong.ants.scenic.data.ScenicItem
import com.yicong.ants.scenic.data.Ticket
import com.yicong.ants.scenic.data.TicketOrder
import com.yicong.ants.scenic.data.WeatherInfo

const val ORDER_NORMAL = 0
const val ORDER_LIKE = 1
const val ORDER_LOCATION = 2

class ScenicViewModel : ViewModel() {
    val cityList = MutableLiveData<LoadStateData<ArrayList<CityInfo>?>?>()
    val childList = MutableLiveData<ArrayList<CityInfo.ChildArea>?>()

    //0城市选择 其他以及1是区县选择
    var selectStatus = 0

    //最终选择的城市区县
    val pCity = MutableLiveData<String?>()
    val pChild = com.yicong.ants.scenic.SingleLiveData<String?>()

    //定位的经度纬度
    var pLa: String? = null
    var pLo: String? = null

    //adapter列表选择状态缓存
    val city = MutableLiveData<CityInfo?>()
    val child = MutableLiveData<CityInfo.ChildArea?>()

    fun getCityList(activity: BaseTitleBarActivity<*>) {
        cityList.value = LoadStateData(state = LoadStateData.LoadState.LOADING)
        activity.addSubscribe(
            RetrofitHelper.getApis().getCityList()
                .compose(RxHelper.toMain())
                .subscribe({ respBean ->
                    if (respBean.isCodeFail) {
                        cityList.value =
                            LoadStateData(state = LoadStateData.LoadState.ERROR, msg = respBean.msg)
                        return@subscribe
                    }
                    cityList.value = LoadStateData(data = respBean.data)
                }, RxHelper.getHideProgressConsumer(activity))
        )
    }

    fun selectCity(data: CityInfo?) {
        data?.let {
            cityList.value?.data?.forEach {
                if (data.id == it.id) {
                    childList.value = it.childArea
                    return@forEach
                }
            }
            city.value = data
        }
    }

    fun getCityPosition(name: String): Int {
        var position = -1
        for (i in 0 until cityList.value!!.data!!.size) {
            if (name == cityList.value!!.data!![i].name) {
                position = i
                break
            }
        }
        return position
    }

    fun getChildInfo(name: String): CityInfo? {
        var cityInfo: CityInfo? = null
        for (i in 0 until cityList.value!!.data!!.size) {
            if (name == cityList.value!!.data!![i].name) {
                cityInfo = cityList.value!!.data!![i]
                break
            }
        }
        return cityInfo
    }

    fun getChildPosition(name: String): Int {
        var position = -1
        for (i in 0 until childList.value!!.size) {
            if (name == childList.value!![i].name) {
                position = i
                break
            }
        }
        return position
    }

    fun getChildData(name: String): CityInfo.ChildArea? {
        var data: CityInfo.ChildArea? = null
        for (i in 0 until childList.value!!.size) {
            if (name == childList.value!![i].name) {
                data = childList.value!![i]
                break
            }
        }
        return data
    }

    //************************************************************************************
    val scenicItemList = OneTimeLiveData<LoadStateData<PageData<ScenicItem>?>?>()
    val ticketOrderList = OneTimeLiveData<LoadStateData<PageData<TicketOrder>?>?>()
    val scenicDetail = MutableLiveData<LoadStateData<ScenicDetail?>?>()
    val ticket = OneTimeLiveData<LoadStateData<Ticket?>?>()
    val ticketOrder = OneTimeLiveData<LoadStateData<TicketOrder?>?>()
    val weatherInfo = OneTimeLiveData<LoadStateData<WeatherInfo?>?>()
    val ticketFriend = OneTimeLiveData<LoadStateData<Ticket?>?>()
    val cancelResult = OneTimeLiveData<LoadStateData<Any?>?>()
    val payResult = OneTimeLiveData<LoadStateData<Any?>?>()

    fun getScenicList(
        activity: BaseTitleBarActivity<*>,
        keyword: String = "",
        sort: Int = ORDER_NORMAL,
        refresh: Boolean,
        cityName: String? = null,
        provinceName: String? = null,
        longitude: String? = null,
        latitude: String? = null
    ) {
        var page = (scenicItemList.value?.data?.pageInfo?.curPage ?: 0).inc()
        if (refresh) {
            page = 1
        }

        (scenicItemList as MutableLiveData<LoadStateData<PageData<ScenicItem>?>?>).value =
            LoadStateData(state = LoadStateData.LoadState.LOADING)
        activity.addSubscribe(
            RetrofitHelper.getApis().getScenicList(
                sort = sort,
                keyWord = keyword,
                pageIndex = page,
                cityName = cityName,
                provinceName = provinceName,
                longitude = longitude,
                latitude = latitude
            )
                .compose(RxHelper.toMain())
                .subscribe({ respBean ->
                    if (respBean.isCodeFail) {
                        (scenicItemList as MutableLiveData<LoadStateData<PageData<ScenicItem>?>?>).value =
                            LoadStateData(state = LoadStateData.LoadState.ERROR, msg = respBean.msg)
                        return@subscribe
                    }
                    (scenicItemList as MutableLiveData<LoadStateData<PageData<ScenicItem>?>?>).value =
                        LoadStateData(data = respBean.data)
                }, RxHelper.getHideProgressConsumer(activity))
        )
    }

    fun getScenicDetail(id: String, activity: BaseTitleBarActivity<*>) {
        scenicDetail.value = LoadStateData(state = LoadStateData.LoadState.LOADING)
        activity.addSubscribe(
            RetrofitHelper.getApis().getScenicDetail(id)
                .compose(RxHelper.toMain())
                .subscribe({ respBean ->
                    if (respBean.isCodeFail) {
                        scenicDetail.value =
                            LoadStateData(state = LoadStateData.LoadState.ERROR, msg = respBean.msg)
                        return@subscribe
                    }
                    scenicDetail.value = LoadStateData(data = respBean.data)
                }, RxHelper.getHideProgressConsumer(activity))
        )
    }

    fun getTicketDetail(id: String, activity: BaseTitleBarActivity<*>) {
        (ticket as MutableLiveData<LoadStateData<Ticket?>?>).value = LoadStateData(state = LoadStateData.LoadState.LOADING)
        activity.addSubscribe(
            RetrofitHelper.getApis().getTicketDetail(id)
                .compose(RxHelper.toMain())
                .subscribe({ respBean ->
                    if (respBean.isCodeFail) {
                        (ticket as MutableLiveData<LoadStateData<Ticket?>?>).value =
                            LoadStateData(state = LoadStateData.LoadState.ERROR, msg = respBean.msg)
                        return@subscribe
                    }
                    (ticket as MutableLiveData<LoadStateData<Ticket?>?>).value =
                        LoadStateData(data = respBean.data)
                }, RxHelper.getHideProgressConsumer(activity))
        )
    }

    fun createOrder(
        activity: BaseTitleBarActivity<*>,
        scenicID: String,
        productID: String,
        travelDate: String,
        friendMobile: String? = null
    ) {
        (ticketOrder as MutableLiveData<LoadStateData<TicketOrder?>?>).value =
            LoadStateData(state = LoadStateData.LoadState.LOADING)
        activity.addSubscribe(
            RetrofitHelper.getApis().createOrder(scenicID, productID, travelDate, friendMobile)
                .compose(RxHelper.toMain())
                .subscribe({ respBean ->
                    if (respBean.isCodeFail) {
                        (ticketOrder as MutableLiveData<LoadStateData<TicketOrder?>?>).value =
                            LoadStateData(state = LoadStateData.LoadState.ERROR, msg = respBean.msg)
                        return@subscribe
                    }
                    (ticketOrder as MutableLiveData<LoadStateData<TicketOrder?>?>).value =
                        LoadStateData(data = respBean.data)
                }, RxHelper.getHideProgressConsumer(activity))
        )
    }

    fun getOrderDetail(activity: BaseTitleBarActivity<*>, customerOrderNo: String) {
        (ticketOrder as MutableLiveData<LoadStateData<TicketOrder?>?>).value =
            LoadStateData(state = LoadStateData.LoadState.LOADING)
        activity.addSubscribe(
            RetrofitHelper.getApis().getOrderDetail(customerOrderNo)
                .compose(RxHelper.toMain())
                .subscribe({ respBean ->
                    if (respBean.isCodeFail) {
                        (ticketOrder as MutableLiveData<LoadStateData<TicketOrder?>?>).value =
                            LoadStateData(state = LoadStateData.LoadState.ERROR, msg = respBean.msg)
                        return@subscribe
                    }
                    (ticketOrder as MutableLiveData<LoadStateData<TicketOrder?>?>).value =
                        LoadStateData(data = respBean.data)
                }, RxHelper.getHideProgressConsumer(activity))
        )
    }

    fun getOrderList(activity: BaseTitleBarActivity<*>, refresh: Boolean) {
        var page = (ticketOrderList.value?.data?.pageInfo?.curPage ?: 0).inc()
        if (refresh) {
            page = 1
        }
        (ticketOrderList as MutableLiveData<LoadStateData<PageData<TicketOrder>?>?>).value =
            LoadStateData(state = LoadStateData.LoadState.LOADING)
        activity.addSubscribe(
            RetrofitHelper.getApis().getOrderList(page)
                .compose(RxHelper.toMain())
                .subscribe({ respBean ->
                    if (respBean.isCodeFail) {
                        (ticketOrderList as MutableLiveData<LoadStateData<PageData<TicketOrder>?>?>).value =
                            LoadStateData(state = LoadStateData.LoadState.ERROR, msg = respBean.msg)
                        return@subscribe
                    }
                    (ticketOrderList as MutableLiveData<LoadStateData<PageData<TicketOrder>?>?>).value =
                        LoadStateData(data = respBean.data)
                }, RxHelper.getHideProgressConsumer(activity))
        )
    }

    fun getWeather(activity: BaseTitleBarActivity<*>, location: String) {
        (weatherInfo as MutableLiveData<LoadStateData<WeatherInfo?>?>).value =
            LoadStateData(state = LoadStateData.LoadState.LOADING)
        activity.addSubscribe(
            RetrofitHelper.getApis().getWeather(location)
                .compose(RxHelper.toMain())
                .subscribe({ respBean ->
                    if (respBean.isCodeFail) {
                        (weatherInfo as MutableLiveData<LoadStateData<WeatherInfo?>?>).value =
                            LoadStateData(state = LoadStateData.LoadState.ERROR, msg = respBean.msg)
                        return@subscribe
                    }
                    (weatherInfo as MutableLiveData<LoadStateData<WeatherInfo?>?>).value =
                        LoadStateData(data = respBean.data)
                }, RxHelper.getHideProgressConsumer(activity))
        )
    }

    fun getFriendInfo(activity: BaseTitleBarActivity<*>, mobile: String) {
        (ticketFriend as MutableLiveData<LoadStateData<Ticket?>?>).value =
            LoadStateData(state = LoadStateData.LoadState.LOADING)
        activity.addSubscribe(
            RetrofitHelper.getApis().getFriendInfo(mobile)
                .compose(RxHelper.toMain())
                .subscribe({ respBean ->
                    if (respBean.isCodeFail) {
                        (ticketFriend as MutableLiveData<LoadStateData<Ticket?>?>).value =
                            LoadStateData(state = LoadStateData.LoadState.ERROR, msg = respBean.msg)
                        return@subscribe
                    }
                    (ticketFriend as MutableLiveData<LoadStateData<Ticket?>?>).value =
                        LoadStateData(data = respBean.data)
                }, RxHelper.getHideProgressConsumer(activity))
        )
    }

    fun cancelOrder(activity: BaseTitleBarActivity<*>, customerOrderNo: String) {
        (cancelResult as MutableLiveData<LoadStateData<Any?>?>).value =
            LoadStateData(state = LoadStateData.LoadState.LOADING)
        activity.addSubscribe(
            RetrofitHelper.getApis().cancelOrder(customerOrderNo)
                .compose(RxHelper.toMain())
                .subscribe({ respBean ->
                    if (respBean.isCodeFail) {
                        (cancelResult as MutableLiveData<LoadStateData<Any?>?>).value =
                            LoadStateData(state = LoadStateData.LoadState.ERROR, msg = respBean.msg)
                        return@subscribe
                    }
                    (cancelResult as MutableLiveData<LoadStateData<Any?>?>).value =
                        LoadStateData(data = respBean.data)
                }, RxHelper.getHideProgressConsumer(activity))
        )
    }

    fun payOrder(activity: BaseTitleBarActivity<*>, customerOrderNo: String, it: String) {
        (payResult as MutableLiveData<LoadStateData<Any?>?>).value =
            LoadStateData(state = LoadStateData.LoadState.LOADING)
        activity.addSubscribe(
            RetrofitHelper.getApis().payOrder(customerOrderNo, it)
                .compose(RxHelper.toMain())
                .subscribe({ respBean ->
                    if (respBean.isCodeFail) {
                        (payResult as MutableLiveData<LoadStateData<Any?>?>).value =
                            LoadStateData(state = LoadStateData.LoadState.ERROR, msg = respBean.msg)
                        return@subscribe
                    }
                    (payResult as MutableLiveData<LoadStateData<Any?>?>).value =
                        LoadStateData(data = respBean.data)
                }, RxHelper.getHideProgressConsumer(activity))
        )
    }
}
